#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 3e5 + 10;
LL f[N][2];
int n;
int a[N], b[N];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i] >> b[i];
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= 1; j++) {
			f[i][j] = f[i - 1][j];
			if (j == 0 && a[i] == 0) f[i][0] = max(f[i][0], max(f[i - 1][0], f[i - 1][1]) + b[i]);
			else f[i][1] = max(f[i][1], f[i - 1][0] + b[i]);
		}
	}
	cout << max(f[n][1], f[n][0]) << endl;
	return 0;
}